﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace CustomWebControls
{
    /// <summary>
    /// Extended TextBox control that applies javascript validation to Multiline TextBox
    /// </summary>
    public class TextArea : TextBox
    {
        /// <summary>
        /// Override PreRender to include custom javascript
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            if (MaxLength > 0 && TextMode == TextBoxMode.MultiLine)
            {
                // Add javascript handlers for paste and keypress
                //Attributes.Add("onkeypress", "doKeypress(this);");
                this.CssClass = "overlaidLength";

                
                // paste test removed temporatily as Chrome does not support them well.
                //Attributes.Add("onbeforepaste", "doBeforePaste(this);");
                //Attributes.Add("onpaste", "doPaste(this);");

                // Add attribute for access of maxlength property on client-side
                Attributes.Add("maxLength", this.MaxLength.ToString());

                //Attributes.Add("title", GetTip());

                // adding Maxlength jquery
                //string script = "<script language=\"javascript\" type=\"text/javascript\">$(function () { $(\"[id*=" 
                //    + this.ClientID + "]\").MaxLength({ MaxLength: " + this.MaxLength.ToString() + " }); });</script>";
                //string scriptname = this.ClientID;

                string script = "<script language=\"javascript\" type=\"text/javascript\">$(function () {$('#" + this.ClientID + "').maxlength({ max: " + this.MaxLength.ToString() + " , feedbackText: '{c}/{m}', showFeedback: 'active' });});</script>";
                string scriptname = this.ClientID;


                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), scriptname, script);

                // Register client side include - only once per page
                if (!Page.ClientScript.IsClientScriptIncludeRegistered("TextArea"))
                {
                    Page.ClientScript.RegisterClientScriptInclude("TextArea", ResolveClientUrl("~/js/textArea.js"));
                }
            }
            base.OnPreRender(e);


        }

        //string GetTip()
        //{
        //    //Your logic code gose here
        //    return "UserInput";
        //}
    }
}
